﻿package igz.fleaxy.ui.preloader {
	
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import igz.fleaxy.util.ObjectUtil;
	
	/**
	 * Clase que crea un preloader escalar simple
	 * 
	 * @param	p_settings : Object = { "frontLayer": displayObject
	 * 								  , "textEnable": Boolean
	 * 								  , "textSize": uint
	 * 								  , "textColor": uint				
	 * 								  , "textDisplace": Point				
	 * 								  , "type": ScalePreloaderType }
	 * 
	 * @example ScalePreloaderUi( {  "frontLayer": displayObject_2
	 * 							   , "textEnable": false
	 * 							   , "textSize": 15
	 * 							   , "textColor": 0xFFFFFF
	 * 							   , "textDisplace": new Point(5, -25);
	 * 							   , "type": ScalePreloaderType.$HORIZONTAL
	 * 							   }
	 * 							 );
	 */
	 
	public class ScalePreloaderUi extends Sprite {
		
		private const _PADDING_TEXT : uint = 10;
		
		private var _MaxW : Number;
		private var _MaxH : Number;
		
		private var _FrontLayer  : Sprite;
		private var _CounterText : TextField; 
		
		private var _Settings : Object = { "frontLayer": null
										,  "textEnable": false
										,  "textSize":  15
										,  "textColor": 0xFFFFFF
										,  "textDisplace": new Point()
										,  "type": ScalePreloaderType.$HORIZONTAL
										};

		
		// Constructora
		public function ScalePreloaderUi( p_settings:Object = null ) {
			
			// Combinamos opciones
			ObjectUtil.$Merge( p_settings, _Settings );
			
			// Capa frontal
			_FrontLayer = new Sprite();
			addChild( _FrontLayer );
			
			if ( _Settings["frontLayer"] == null ) {
				_Load_DefaultPreloader();
			}
			else {
				_Load_CustomPreloader();
			}
			
		}
	
		
		// Método que establece un cargador por defecto
		private function _Load_DefaultPreloader() : void {
			
			_MaxW = 200;
			_MaxH = 25;
			
			// Capa frontal
			_FrontLayer.graphics.lineStyle( 1, 0xFFFFFF, 1, true );
			_FrontLayer.graphics.beginFill( 0x084B8A, 1 );
			_FrontLayer.graphics.drawRect( 0, 0, _MaxW, _MaxH );
			_FrontLayer.graphics.endFill();
		
			// Contador de completado
			_Create_CounterText();
		}
		
		
		// Método que establece un cargador personalizado
		private function _Load_CustomPreloader() : void {
			
			// Capa frontal
			_FrontLayer.addChild( _Settings["frontLayer"] );
			
			_MaxW = _FrontLayer.width;
			_MaxH = _FrontLayer.height;
			
			// Contador de completado
			if ( _Settings["textEnable"] ) {
				_Create_CounterText();
			}

		}
		
		
		// Método que establece un contador prar el cargador
		private function _Create_CounterText() : void {
			_CounterText = new TextField();
			_CounterText.defaultTextFormat = new TextFormat( "Verdana", _Settings["textSize"],_Settings["textColor"] );
	//		_CounterText.textColor = _Settings["textColor"];
			_CounterText.selectable = _CounterText.multiline = false;
			addChild( _CounterText );
		}
		
		// Metodo que asigna un valor al cargador
		public function $SetValue( p_value:uint ) : void {
			_UpdatePreloader( p_value );
		}
		
		private function _UpdatePreloader( p_value:uint ) : void {
			
			switch ( _Settings["type"] ) {
				case LinearPreloaderType.$HORIZONTAL : _ScaleX( p_value ); break;
				case LinearPreloaderType.$VERTICAL   : _ScaleY( p_value ); break;
			}
						
			if ( _CounterText ) {
				_Update_CounterText( p_value );
			}
		}
		
		protected function _Update_CounterText( p_value:uint ) : void {
			_CounterText.htmlText = String( p_value + " % ");
		}
		
		private function _ScaleX( p_value:uint ) : void {
			_FrontLayer.scaleX = p_value / _MaxW; 
			_FrontLayer.x = p_value - p_value;
			_CounterText.x = _FrontLayer.x + _FrontLayer.width + _PADDING_TEXT + _Settings["textDisplace"].x;
			_CounterText.y = ( _FrontLayer.height - _CounterText.textHeight ) * 0.5 + _Settings["textDisplace"].y;
		}
		
		private function _ScaleY( p_value:uint ) : void {
			_FrontLayer.scaleY = ( p_value / _MaxH );
			_FrontLayer.y = _MaxH - p_value;
			_CounterText.x = ( _FrontLayer.width  - _CounterText.textWidth ) * 0.5 + _Settings["textDisplace"].x;
			_CounterText.y = _FrontLayer.y - _PADDING_TEXT*2 + _Settings["textDisplace"].y;
		}
		
	}

}